/*
 *  Copyright (c) Skrumtious
 */
package com.skrumtious.backend.memory;

import java.util.List;
import java.util.Map;
import java.util.Vector;
import java.util.concurrent.ConcurrentHashMap;

import com.skrumtious.backend.DoAddSkrumItemRequest;
import com.skrumtious.backend.DoAddSkrumItemResponse;
import com.skrumtious.backend.DoAddSkrumTabResponse;
import com.skrumtious.backend.DoAddUserRequest;
import com.skrumtious.backend.DoAddUserResponse;
import com.skrumtious.backend.DoGetSkrumListRequest;
import com.skrumtious.backend.DoGetSkrumListResponse;
import com.skrumtious.backend.DoRemoveSkrumItemRequest;
import com.skrumtious.backend.DoRemoveSkrumItemResponse;
import com.skrumtious.backend.DoRemoveSkrumTabResponse;
import com.skrumtious.backend.DoReorderSkrumItemRequest;
import com.skrumtious.backend.DoReorderSkrumItemResponse;
import com.skrumtious.backend.SkrumService;
import com.skrumtious.backend.persistence.SkrumItem;
import com.skrumtious.backend.persistence.SkrumList;
import com.skrumtious.backend.persistence.SkrumTab;

/**
 * @author Tony
 *
 */
public class InMemorySkrumService implements SkrumService {
	
	/**
	 * In memory database
	 */
	private Map<String, List<SkrumTab>> database;
	
	final String TMP_USER = "tonysparks";
	
	public InMemorySkrumService() {
		this.database = new ConcurrentHashMap<String, List<SkrumTab>>();
		this.database.put(TMP_USER, new Vector<SkrumTab>());
						
		/* The default tab */
		SkrumTab skrumTab = new SkrumTab("Tony", 0, new SkrumList(), new SkrumList());
		this.database.get(TMP_USER).add(skrumTab);
	}
	
	/* (non-Javadoc)
	 * @see com.skrumtious.backend.SkrumService#getSkrumList(com.skrumtious.backend.DoGetSkrumListRequest)
	 */
	@Override
	public DoGetSkrumListResponse getSkrumList(DoGetSkrumListRequest request)
			throws Exception {
		
		return new DoGetSkrumListResponse(true, "", "", this.database.get(request.getUserId()));
	}

	/* (non-Javadoc)
	 * @see com.skrumtious.backend.SkrumService#addSkrumItem(com.skrumtious.backend.DoAddSkrumItemRequest)
	 */
	@Override
	public DoAddSkrumItemResponse addSkrumItem(DoAddSkrumItemRequest request)
			throws Exception {
		SkrumTab skrumTab = this.database.get(request.getUserId()).get(request.getTabId());
		if ( skrumTab != null ) {		
			skrumTab.getActiveList().addSkrumItem(request.getSkrumItem());
		}
		return new DoAddSkrumItemResponse(true, "", "");
	}

	/* (non-Javadoc)
	 * @see com.skrumtious.backend.SkrumService#removeSkrumItem(com.skrumtious.backend.DoRemoveSkrumItemRequest)
	 */
	@Override
	public DoRemoveSkrumItemResponse removeSkrumItem(
			DoRemoveSkrumItemRequest request) throws Exception {
		
		DoRemoveSkrumItemResponse response = null;
		
		SkrumTab skrumTab = this.database.get(request.getUserId()).get(request.getTabId());
		if ( skrumTab != null ) {
		
			SkrumItem deactivated = skrumTab.getActiveList().removeSkrumItem(request.getItemId());
			if ( deactivated != null ) {
				deactivated.setActive(false);
				skrumTab.getDeactiveList().addSkrumItem(deactivated);
				
				response = new DoRemoveSkrumItemResponse(true, "", "");
			}
		}
		
		if ( response == null ) {
			response = new DoRemoveSkrumItemResponse(false, "", "Unable to remove the skrum item!");
		}
				
		return response;
	}

	/* (non-Javadoc)
	 * @see com.skrumtious.backend.SkrumService#addSkrumTab(com.skrumtious.backend.DoAddSkrumItemRequest)
	 */
	@Override
	public DoAddSkrumTabResponse addSkrumTab(DoAddSkrumItemRequest request)
			throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	/* (non-Javadoc)
	 * @see com.skrumtious.backend.SkrumService#removeSkrumTab(com.skrumtious.backend.DoRemoveSkrumItemRequest)
	 */
	@Override
	public DoRemoveSkrumTabResponse removeSkrumTab(
			DoRemoveSkrumItemRequest request) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	/* (non-Javadoc)
	 * @see com.skrumtious.backend.SkrumService#addUser(com.skrumtious.backend.DoAddUserRequest)
	 */
	@Override
	public DoAddUserResponse addUser(DoAddUserRequest request) {
		// TODO Auto-generated method stub
		return null;
	}

	/* (non-Javadoc)
	 * @see com.skrumtious.backend.SkrumService#reorderSkrumItem(com.skrumtious.backend.DoReorderSkrumItemRequest)
	 */
	@Override
	public DoReorderSkrumItemResponse reorderSkrumItem(
			DoReorderSkrumItemRequest request) throws Exception {

		DoReorderSkrumItemResponse response = null;
		
		int[] order = request.getOrder();
		
		try {
			SkrumTab skrumTab = this.database.get(request.getUserId()).get(request.getTabId());
			if ( skrumTab != null ) {
				SkrumList list = skrumTab.getActiveList();
				list.reorder(order);
				
				response = new DoReorderSkrumItemResponse(true, "", "");
			}
			else {
				response = new DoReorderSkrumItemResponse
					(false, "", "Unable to do reorder because the tab: " + request.getTabId() + " isn't present.");
			}
		}
		catch(Exception e) {
			response = new DoReorderSkrumItemResponse
					(false, e.getMessage(), "Unable to do reorder: " + e.getMessage()); 
		}
				
		return response;
	}

}
